<?php
/*
---------------------------------------------------------
														+
名称:易通支付通道										+
														+
控制器名称:YiTongController								+
														+
日期:2017-10-11											+
														+
作者:小翁												+
														+
---------------------------------------------------------
*/
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

/**
 * 随心付
 * Class SuixinfuController
 * @package Pay\Controller
 */
class SuixinfuController extends PayController{
    protected $config;
    protected $key;
    protected $url;
    protected $class;


    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));

    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);
        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $ismobile = isMobile();
        $return = $this->orderadd( $parameter );

        switch($this->config['paytype']){
            //支付宝扫码
            case 3:
            case 4:
                $pay_bankcode = 'AliSao';
                break;
            default:
                $this->showmessage('不支持此产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $data['txnamt'] = strval(intval($return['amount']*100));
        $data['merid'] = $this->config['mch_id'];
        $data['orderid'] = $return['orderid'];
        $data['backurl'] = $return['notifyurl'];
        $data['fronturl'] = $return['callbackurl'];
        $data['action'] = $pay_bankcode;

        //$json = json_encode($data);
        $json = json_encode($data,JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

        $str = base64_encode($json) . $this->config['signkey'];

        $post = [];
        $post['req'] = base64_encode($json);
        $post['sign'] = md5($str);
        $res = curl($this->config['gateway'], httpstr($post));//curl_post

        $resp = json_decode($res,true);

        $result = json_decode(base64_decode($resp['resp']), true);

        if(!$result['formaction']){
            if($result['respmsg']){
                $this->showmessage($result['respmsg'],['code'=>C("THIRDCODES.".$this->class)]);
            }

            $this->showmessage('获取支付链接失败!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        header("Location: {$result['formaction']}");exit();
    }



    //通知
    public function notifyurl(){
        //$this->debug_callback();

        //上游回调IP白名单
        if(!$this->checkip(getIP(),['120.25.215.97'])){
            exit("wrong");
        }

        $p = $origin = file_get_contents('php://input');

        parse_str($p, $p);
        $resp = $p['resp'];
        $p['resp'] = json_decode(base64_decode($p['resp']),true);

        //获取订单信息
        $orderid = $p['resp']['orderid'];

        //获取订单
        $find = $this->checkOrderNotify($orderid);

        if(!$find){
            echo 'order_not_found'; exit;
        }
        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(

            "pay_orderid_rd"=>htmlspecialchars($p['resp']['queryid'])));

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();

        $sign = $p['sign'];
        $sign2 = md5($resp . $this->config['signkey']);


        if($sign2 !== $sign ){
            exit("sign_wrong");
        }

        if($p['resp']['resultcode'] !== "0000"){
            exit("pay_fail");
        }

        $this->async($find['pay_orderid'],"0000");
    }

    public function callBackUrl(){

        echo '支付成功'; exit;
    }
}